package com.changhai.diabetes.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.changhai.diabetes.model.OpertResultMsg;
import com.changhai.diabetes.model.QueryDTO;
import com.changhai.diabetes.model.ResearchGroup;
import com.changhai.diabetes.model.TableForPatient;
import com.changhai.diabetes.model.User;
import com.changhai.diabetes.model.page.Page;
import com.changhai.diabetes.service.GroupService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 科研入组管理
 * 
 * @author Andy
 * 
 */
public class GroupAction extends BasePaginationAction {

	private static final long serialVersionUID = 4392936553263004630L;

	private OpertResultMsg opertMsg;
	private String orderCol;// 排序字段
	private Long researchgroupid;
	private ResearchGroup researchGroup;
	private List<ResearchGroup> subGroups;
	private GroupService groupService;
	private String[] groupbh;
	private String[] groupname;
	private List<TableForPatient> tbList;
	private User user;
	
	public String execute() throws Exception {
		return super.execute();
	}

	public String group() {
		return this.query();
	}

	@Override
	protected Page searchQuery(QueryDTO queryDTO) {
		Map map = new HashMap();
		this.setSortInfo(this.getOrderCol(), this.getPage(), map);// 设置排序信息
		queryDTO.setConditions(map);
		user = (User) ActionContext.getContext().getSession().get("user");
		return groupService.searchQuery(queryDTO);
	}

	public String editGroup() {
		if (researchgroupid != null && researchgroupid > 0) {
			try {
				researchGroup = groupService.searchGroupById(researchgroupid);
				Long pid = researchGroup.getParentgroup();
				if(pid!=null && pid==0){
					subGroups = groupService.searchSubGroupbyParentId(researchGroup.getResearchgroupid());
				}
				tbList =  groupService.searchTables();
				return "input";
			} catch (Exception e) {
				e.printStackTrace();	 
			}		
		}
		return "query";
	}

	public String saveEditHospital(){
		try {
			groupService.saveEditHospital(user);
			ActionContext.getContext().getSession().put("user", user);
			opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
		} catch (Exception e) {
			e.printStackTrace();
			opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
		}
		return this.query();
	}
	
	public String saveEditGroup() {
		if (researchgroupid == null || researchgroupid <= 0 || researchGroup == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				groupService.saveEditGroup(researchGroup, groupbh, groupname);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editGroup();
	}

	public String insertNewGroup() {
		tbList =  groupService.searchTables();
		return "input";
	}

	public String saveNewGroup() {
		if (researchGroup == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
			return this.insertNewGroup();
		} else {
			try {
				researchgroupid = groupService.saveNewGroup(researchGroup);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
				return this.insertNewGroup();
			}
		}
		return this.editGroup();
	}

	public OpertResultMsg getOpertMsg() {
		return opertMsg;
	}

	public void setOpertMsg(OpertResultMsg opertMsg) {
		this.opertMsg = opertMsg;
	}

	public GroupService getGroupService() {
		return groupService;
	}

	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	public ResearchGroup getResearchGroup() {
		return researchGroup;
	}

	public void setResearchGroup(ResearchGroup researchGroup) {
		this.researchGroup = researchGroup;
	}

	public Long getResearchgroupid() {
		return researchgroupid;
	}

	public void setResearchgroupid(Long researchgroupid) {
		this.researchgroupid = researchgroupid;
	}

	public String getOrderCol() {
		return orderCol;
	}

	public void setOrderCol(String orderCol) {
		this.orderCol = orderCol;
	}

	public String[] getGroupbh() {
		return groupbh;
	}

	public void setGroupbh(String[] groupbh) {
		this.groupbh = groupbh;
	}

	public String[] getGroupname() {
		return groupname;
	}

	public void setGroupname(String[] groupname) {
		this.groupname = groupname;
	}

	public List<TableForPatient> getTbList() {
		return tbList;
	}

	public void setTbList(List<TableForPatient> tbList) {
		this.tbList = tbList;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<ResearchGroup> getSubGroups() {
		return subGroups;
	}

	public void setSubGroups(List<ResearchGroup> subGroups) {
		this.subGroups = subGroups;
	}
}
